<?php

namespace app\admin\controller;
use app\admin\model\NewsModel;
use app\admin\model\NewsCateModel;
use app\admin\model\MemberModel;
use app\admin\model\MemberGroupModel;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Db;

class News extends Base
{

    /**
     * [index 文章列表]
     * @author 【朱沪宁】
     */
    public function index(){

        $key = input('key');
        $cate_id = input('cate_id');
        $map = [];
        $catemap=[];
        $rolename=session('rolename');
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];          
        }  
        
        if($cate_id&&$cate_id!==""){
            $map['think_news.cate_id'] = $cate_id;          
        } else{
            if($rolename=="审核人"){
                if(session('newcateid')&&session('newcateid')!==""){
                    $map['think_news.cate_id']=array('in',''.session('newcateid').'');
                }else{
                    $map['think_news.cate_id']=0;
                }
                
            }  
        }
        /*if($rolename=="发稿人"){
            $map['think_news.uid']=session('uid');
            $this->assign('role', 1);
        } */  
        
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('news')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $article = new NewsModel();
        $lists = $article->getArticleByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('val', $key);
        $this->assign('cate_id', $cate_id);

        $newscate=new NewsCateModel();
		if($rolename!=='超级管理员'){
			$catemap['think_news_cate.id']=array('in',''.session('newcateid').'');
		}
        
        $catelist=$newscate->getXwCate($catemap);
        $this->assign('catelist', $catelist);

        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    public function index_shenhe(){

        $key = input('key');
        $cate_id = input('cate_id');
        $map = [];
        $catemap=[];
        $rolename=session('rolename');
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];          
        }  
        
        if($cate_id&&$cate_id!==""){
            $map['think_news.cate_id'] = $cate_id;          
        } else{
            if($rolename=="审核人"){
                if(session('newcateid')&&session('newcateid')!==""){
                    $map['think_news.cate_id']=array('in',''.session('newcateid').'');
                }else{
                    $map['think_news.cate_id']=0;
                }
                
            }  
        }
        /*if($rolename=="发稿人"){
            $map['think_news.uid']=session('uid');
        } */  
        $map['think_news.status']=0; 
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('news')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $article = new NewsModel();
        $lists = $article->getArticleByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('val', $key);
        $this->assign('cate_id', $cate_id);

        $newscate=new NewsCateModel();
        if($rolename!=='超级管理员'){
			$catemap['think_news_cate.id']=array('in',''.session('newcateid').'');
		}
        $catelist=$newscate->getXwCate($catemap);
        $this->assign('catelist', $catelist);

        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * [add_article 添加文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function add_news()
    {
        if(request()->isAjax()){

            $param = input('post.');

            $article = new NewsModel();
            $param['uid'] = session('uid'); 
            if(session('rolename')!='平台管理员'&&session('rolename')!='超级管理员')
                $param['status']=0;
            $flag = $article->insertArticle($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $cate = new NewsCateModel();
        $map = [];
        //$map['id'] = ['eq',"3"];  
        if(session('rolename')!='超级管理员'){
            if(session('newcateid')&&session('newcateid')!==""){
                $map['id']=array('in',session('newcateid'));
            }else{
                $map['id']=0;
            }
            $this->assign('cate',$cate->getXwCate($map));
        }else{
            $this->assign('cate',$cate->getAllCate());
        }
            
        return $this->fetch();
    }

    public function shenhe_news(){
        if(request()->isAjax()){
            $param = input('post.');
            $newsmodel=new NewsModel();
            $sbArrId=$param["newids"];          
            $sbArr=explode(',',$sbArrId); 
            $tempArr=array(); 
            $count= count($sbArr);
            //用于进行成功调拨的次数统计
            $index=0;
            foreach ($sbArr as $a) {
                $temp=$a;
                $tempArr["status"]=1;
                $tempArr["id"]=$temp;
                $flag1 = $newsmodel->updateArticle($tempArr);
                if($flag1["code"]==1)
                {
                    $index++;
                }
             }
             if( $index==$count)
               {
               return json(['code' => 1, 'data' =>"", 'msg' =>"新闻审核成功"]);
               }else{
               return json(['code' => 0, 'data' =>"", 'msg' =>"部分新闻审核失败,请重新审核"]);
               }

        }
    }

    //查看
    public function find_news()
    {
        $article = new NewsModel();   

        $id = input('param.article_id');
        //dump( $id);
        $cate = new NewsCateModel();
       
        $article = $article->getOneArticle($id);
       
        $this->assign('cate',$cate->getAllCate());
        $this->assign('article',$article);
        return $this->fetch();
    }


    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function edit_news()
    {
        $article = new NewsModel();     
        if(request()->isAjax()){

            $param = input('post.');
            //$param["create_time"] = strtotime($param["create_time"]);  
            $flag = $article->updateArticle($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.article_id');
        //dump( $id);
        $cate = new NewsCateModel();
       
        $article = $article->getOneArticle($id);
       
        $this->assign('cate',$cate->getAllCate());
        $this->assign('article',$article);
        return $this->fetch();
    }



    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function del_news()
    {
        $id = input('param.id');
        $cate = new NewsModel();
        $flag = $cate->delArticle($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function get_members()
    {
        //halt("111");
        $flag = array();
        $cate =$_GET['group_id'];
        //halt($cate);
        $flag = Db::name('member')->where(array('group_id'=> $cate))->field('id,nickname')->order("nickname desc")->select();
        //$flag = Db::name('member')->field('id,nickname')->select();
        //$this->ajaxReturn($result,"JSON");
         return json($flag);
    }

    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function news_state()
    {
        $id=input('param.id');
        $status = Db::name('news')->where(array('id'=>$id))->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('news')->where(array('id'=>$id))->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('news')->where(array('id'=>$id))->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }


     

    //*********************************************分类管理*********************************************//

    /**
     * [index_cate 分类列表]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function index_cate(){

        $cate = new NewsCateModel();
        $list = $cate->getAllCate();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * [add_cate 添加分类]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function add_cate()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new NewsCateModel();
            $flag = $cate->insertCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }


    /**
     * [edit_cate 编辑分类]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function edit_cate()
    {
        $cate = new NewsCateModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $this->assign('cate',$cate->getOneCate($id));
        return $this->fetch();
    }


    /**
     * [del_cate 删除分类]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function del_cate()
    {
        $id = input('param.id');
        $cate = new NewsCateModel();
        $flag = $cate->delCate($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [cate_state 分类状态]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function cate_state()
    {
        $id=input('param.id');
        $status = Db::name('news_cate')->where(array('id'=>$id))->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('news_cate')->where(array('id'=>$id))->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('news_cate')->where(array('id'=>$id))->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }  

    public function index_add_user_cate(){

        $key = input('key');
        $id = input('id');
        $map = [];
        if($key&&$key!==""){
            $map['think_admin.username'] = ['like',"%" . $key . "%"];          
        }  
        $usermodel=new UserModel();
        $cate = new NewsCateModel();
        $cate=$cate->getOneCate($id);
        $uid=0;
        if($cate['userid']&&$cate['userid']!==""){
            //$map['think_admin.id']=array(' notin','' .$cate['uid']. '');
            $uid=$cate['userid'];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $lists = $usermodel->getUsersByWhereUid($map, $Nowpage, $limits,$uid);
        $count = $usermodel->getUsersByWhereCount($map,$uid);//计算总页面
        $allpage = intval(ceil($count / $limits));
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        $this->assign('cateid', $id);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    public function add_userid(){
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new NewsCateModel();
            $cate=$cate->getOneCate($param['id']);
            if($cate['userid']&&$cate['userid']!==""){
                $param['userid']=$cate['userid'].",".$param['userid'];
            }
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }

    public function index_del_user_cate(){

        $key = input('key');
        $id = input('id');
        $map = [];
        if($key&&$key!==""){
            $map['think_admin.username'] = ['like',"%" . $key . "%"];          
        }  
        $usermodel=new UserModel();
        $cate = new NewsCateModel();
        $cate=$cate->getOneCate($id);
        
        if($cate['userid']&&$cate['userid']!==""){
            $map['think_admin.id']=array('in','' .$cate['userid']. '');
            //$uid=$cate['userid'];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 500;// 获取总条数
        $lists = $usermodel->getUsersByWhereCateId($map, $Nowpage, $limits);
        
        $count = $usermodel->getUsersByWhereCateIdCount($map);//计算总页面
        $allpage = intval(ceil($count / $limits));
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        $this->assign('cateid', $id);
        $this->assign('lists', $lists);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    public function del_userid(){
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new NewsCateModel();
            //$cate=$cate->getOneCate($param['id']);
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }

}